package com.example.module_merchant.activity

import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.module_merchant.activity.AddEnterpriseActivity
import com.bw.module_merchant.activity.EnterpriseDetailActivity
import com.bw.module_merchant.adapter.EnterpriseAdapter
import com.bw.module_merchant.vm.EnterpriseViewModel
import com.example.lib_common.view.view.BaseMvvmActivity
import com.example.module_merchant.R
import com.example.module_merchant.databinding.ActivityEnterpriseBinding

import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
@Route(path = "/merchant/EnterpriseActivity")
class EnterpriseActivity : BaseMvvmActivity<ActivityEnterpriseBinding>() {

    private val viewModel : EnterpriseViewModel by viewModels()
    private val enterAdapter = EnterpriseAdapter()

    override fun initViews() {


        binding.rv.adapter = enterAdapter
        binding.rv.layoutManager = LinearLayoutManager(this)

        binding.enterpriseBack.setOnClickListener {
            finish()
        }

        binding.enterpriseAdd.setOnClickListener {
            startActivity(Intent(this , AddEnterpriseActivity::class.java))
        }

        viewModel.getEnterpriseList()

        enterAdapter.setOnItemClickListener { adapter, view, position ->
            startActivity(Intent(this , EnterpriseDetailActivity::class.java))
            SPUtils.getInstance().put("enterpriseId" , enterAdapter.data[position].id)

        }

        viewModel.mSuccessLiveData.observe(this){
            enterAdapter.data.clear()
            enterAdapter.data.addAll(it.data)
            enterAdapter.notifyDataSetChanged()
        }

        viewModel.mErrorLiveData.observe(this){
            ToastUtils.showShort(it)
        }

    }


    override fun getLayoutId(): Int= R.layout.activity_enterprise



    override fun getActivityState(state: Bundle) {
        TODO("Not yet implemented")
    }

    override fun saveActivityState(outState: Bundle) {
        TODO("Not yet implemented")
    }


}